import Jsonp from 'jsonp'
import axios from 'axios'
import { message } from 'antd';
// import Utils  from '../utils/utils';
export default class Axios {

    // 跨域请求封装
    static jsonp(options) {
        return new Promise((resolve, reject) => {
        Jsonp(options.url, {
            param: 'callback'
        }, function (err, response) {
                //todo
                if (response.status === 'success') {
                resolve(response)
                } else {
                reject(response.message)
                }
            })
        })
    }

    
    //axios请求封装
    static ajax(options) {
        let loading
        if (options.data && options.data.isShowLoading !== false) {
            loading = document.getElementById('ajaxLoading');
            loading.style.display = 'block';
        }
        let baseApi = 'https://www.easy-mock.com/mock/5ca2be52d8c59217b63528e2/reactApi'
        return new Promise((resolve, reject) => {
            axios({
                url: options.url,
                method: options.method || 'get',
                baseURL: baseApi,
                timeout: 5000,
                params: (options.data && options.data.params) || ''
            }).then((response) => {
                if (options.data && options.data.isShowLoading !== false) {
                    loading = document.getElementById('ajaxLoading');
                    loading.style.display = 'none';
                }
                if (response.status === 200) { // http请求成功
                    let res = response.data
                    if (res.code === '0') { // 业务代码请求成功
                        resolve(res)
                    } else {
                        message.info(res.msg);
                    }
                } else {
                    reject(response.data)
                }
            })
        })
    }
}